<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="demo1"></div>


    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    
    <script type="text/babel">
        class Life extends React.Component {

            state = {
                opacity: 1
            }

            // 组件挂载完毕调用
            componentDidMount(){
                this.timer = setInterval(()=>{
                    let {opacity} = this.state
                    opacity -= 0.1
                    if(opacity<=0){
                        opacity = 1
                    }
                    this.setState({opacity})
                },200)
            }

            render(){
                return (
                    <div>
                        <h2 style={{opacity: this.state.opacity} } >react</h2>
                        <button onClick={this.remove}>移除</button>
                    </div>
                )
            }

            remove = ()=>{
                // 清空定时器
                clearInterval(this.timer)
                // 卸载组件
                ReactDOM.unmountComponentAtNode(document.getElementById('demo1'))
            }

            // 组件卸载之前
            componentWillUnmount(){

            }

        }
        ReactDOM.render(<Life/>, document.getElementById('demo1'))
    </script>
</body>
</html>